from django.db import models


# Create your models here.
class Activity(models.Model):
    """
    动态表
    user-用户
    time-发表时间
    is_share-是否为转发
    share_id-转发的动态id
    share_count-转发数
    like_count-点赞数量
    """
    user = models.ForeignKey('account_sys.User', on_delete=models.DO_NOTHING)
    time = models.DateTimeField(null=False, verbose_name=u"动态发布时间")
    content = models.TextField(default="发表动态")
    is_share = models.BooleanField(default=False)
    share_id = models.IntegerField(null=True)
    share_count = models.IntegerField(default=0)
    like_count = models.IntegerField(default=0)
    comment_count = models.IntegerField(default=0)
    is_delete = models.BooleanField(default=False)

    def parse_dict(self):
        shareChain = []
        now_is_share = self.is_share
        now_share_id = self.share_id
        is_delete = False
        while now_is_share:
            # print(now_share_id)
            now_activity = Activity.objects.get(id=now_share_id)
            now_share_id = now_activity.share_id
            now_is_share = now_activity.is_share
            shareChain.append({
                "userId": now_activity.user_id,
                "content": now_activity.content
            })
            if not now_is_share:  # 找到源动态
                now_share_id = now_activity.id
                is_delete = now_activity.is_delete
                break
        return {
            "postId": self.id,
            "userId": self.user_id,
            "avatar": self.user.avatar,
            "username": self.user.name,
            "time": self.time,
            "content": self.content,
            "isShared": self.is_share,
            "shareId": now_share_id,
            "originPostIsDelete": is_delete,
            "isDelete": self.is_delete,
            "likesCount": self.like_count,
            "commentsCount": self.comment_count,
            "sharesCount": self.share_count,
            "shareChain": shareChain
        }


class ActivityImage(models.Model):
    image_key = models.CharField(max_length=100, default='')
    belong_activity = models.ForeignKey(Activity, on_delete=models.CASCADE)


class Like(models.Model):
    """
    点赞表
    user-点赞的用户
    time-点赞的时间
    belong_activity_id-赞的动态
    """
    user = models.ForeignKey('account_sys.User', on_delete=models.DO_NOTHING)
    time = models.DateTimeField(null=False, verbose_name=u"点赞时间")
    belong_activity = models.ForeignKey(Activity, on_delete=models.CASCADE)


class Comment(models.Model):
    user = models.ForeignKey('account_sys.User', on_delete=models.DO_NOTHING)
    time = models.DateTimeField(null=False, verbose_name=u"动态发布时间")
    content = models.TextField(null=False)
    belong_activity = models.ForeignKey(Activity, on_delete=models.CASCADE)

    def parse_dict(self):
        return {
            "cId": self.id,
            "username": self.user.name,
            "userId": self.user.id,
            "time": self.time,
            "content": self.content,
            "avatar": self.user.avatar
        }
